<?php

namespace app\admin\controller\sensitive;

use app\common\controller\Backend;
use fast\Tree;
use think\Db;

/**
 * 敏感词管理
 *
 * @icon fa fa-circle-o
 */
class Words extends Backend
{

    /**
     * Words模型对象
     * @var \app\admin\model\sensitive\Words
     */
    protected $model = null;
	protected $wordCategoryList = [];
	
	/**
	 * @Author   : crafters@qq.com
	 * @DateTime : 2022-3-6 18:43
	 * @Introduce: 控制器初始化
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
    public function _initialize()
    {
		
        parent::_initialize();
		
        $this->model = new \app\admin\model\sensitive\Words;
		
	    $this->wordCategoryList = Db::name('sensitive_words_category')->field('id, cate_name')->select();
		$wordCategoryList = ['' => __('None')];
	
	    foreach ($this->wordCategoryList as $k => &$v) {
		    $wordCategoryList[$v['id']] = $v['cate_name'];
	    }
	
	    $level_list = [
		    '' => __('None'),
			1=>'一级敏感',
			2=>'二级敏感',
			3=>'三级敏感',
			4=>'四级敏感',
	    ];
		
	    $this->view->assign('level_list', $level_list);
	    $this->view->assign('wordcategory_data', $wordCategoryList);
		
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            [$where, $sort, $order, $offset, $limit] = $this->buildparams();

            $list = $this->model
                    ->with(['category'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                
                $row->getRelation('category')->visible(['cate_name']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }
	
	
	public function catelist()
	{
		$WordCategoryListModel = Db::name('sensitive_words_category')->field('id, cate_name')->select();
		$wordCategoryList = [];
		
		foreach ($WordCategoryListModel as $k => &$v) {
			$wordCategoryList[$v['id']] = $v['cate_name'];
		}
		
		return json($wordCategoryList);
	}
	
	
	/**
	 * 添加
	 */
	public function add()
	{
		if ($this->request->isPost()) {
			$this->token();
		}
		
		
		
		return parent::add();
	}
	
	/**
	 * 编辑
	 */
	public function edit($ids = null)
	{
		if ($this->request->isPost()) {
			$this->token();
		}
		return parent::edit($ids);
	}
	
	/**
	 * 删除
	 */
	public function del($ids = "")
	{
		if (!$this->request->isPost()) {
			$this->error(__("Invalid parameters"));
		}
		$ids = $ids ? $ids : $this->request->post("ids");
		if ($ids) {
			$delIds = [];
			foreach (explode(',', $ids) as $k => $v) {
				$delIds = array_merge($delIds, [$v]);
			}
			$delIds = array_unique($delIds);
			$count = $this->model->where('id', 'in', $delIds)->delete();
			if ($count) {
				$this->success();
			}
		}
		$this->error();
	}
	
	
}
